package com.erlinyou.im.service.chat.impl;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.stereotype.Service;

import com.erlinyou.base.service.BaseServiceImpl;
import com.erlinyou.im.dao.repository.chat.ChatSessionRepository;
import com.erlinyou.im.dao.repository.chat.ChatMessageRepository;
import com.erlinyou.im.dao.repository.offline.OfflineMessageRepository;
import com.erlinyou.im.model.chat.ChatSession;
import com.erlinyou.im.model.chat.ChatMessage;
import com.erlinyou.im.model.offline.OfflineMessage;
import com.erlinyou.im.service.chat.IChatMessageService;

/**
 *
 */
@Service
public class ChatMessageServiceImpl extends BaseServiceImpl<ChatMessage, String> implements IChatMessageService {
	@Resource
	private OfflineMessageRepository offlineMessageRepository;
	@Resource
	private ChatSessionRepository chatContactsRepository;
	
	private ChatMessageRepository repository;
	@Resource
	public void setBaseService(ChatMessageRepository repository) {
		this.repository = repository;
		this.baseRepository = repository;
	}
	
	@Override
	public boolean existsByClientid(String clientid) {
		return repository.existsByClientid(clientid);
	}
	@Override
	@Transactional
	public boolean saveMessage(ChatMessage chatMessage, List<OfflineMessage> offlineMessages, List<ChatSession> chatContacts) throws RuntimeException {
		boolean zt = repository.save(chatMessage)!=null;
		if(zt){
			boolean oflineZt = offlineMessageRepository.saveAll(offlineMessages).size()>0;
				for(ChatSession item:chatContacts){
					int upNum = chatContactsRepository.updateChatSession(item);
					if(upNum<=0){
						throw new RuntimeException();
					}
				}
				return true;
		}else{
			throw new RuntimeException();
		}
	}
	@Override
	@Transactional
	public boolean saveMessage(ChatMessage chatMessage, List<OfflineMessage> offlineMessages) throws RuntimeException {
		boolean zt = repository.save(chatMessage)!=null;
		if(zt){
			boolean oflineZt = offlineMessageRepository.saveAll(offlineMessages).size()>0;
			return true;
		}else{
			throw new RuntimeException();
		}
	}
	@Override
	@Transactional
	public boolean saveMessage(List<OfflineMessage> offlineMessages) throws RuntimeException {
		boolean oflineZt = offlineMessageRepository.saveAll(offlineMessages).size()>0;
		if(oflineZt){
			return true;
		}else{
			throw new RuntimeException();
		}
	}
	@Override
	@Transactional
	public void revokeMessage(ChatMessage chatMessage,List<ChatSession> chatSession) {
		repository.save(chatMessage);
		
		offlineMessageRepository.deleteByMsgid(chatMessage.getId());//删除离线消息
		
		for(ChatSession item:chatSession){
			chatContactsRepository.updateRevokeChatSession(item);
		}
	}
	@Override
	public List<ChatMessage> getMsg(String userId, String gid,Long inGourpTime, Integer start, Integer page,int ctype) {
		List<ChatMessage> msglist = repository.getMsg(gid,inGourpTime, start, page,ctype);
		List<ChatMessage> msgDel = repository.getMsgDel(userId);
		msglist.removeAll(msgDel);
		return msglist;
	}
	@Override
	public Integer getCount(String userid, String gid) {
		return repository.getCount(userid, gid);
	}
/*
	@Override
	public List<ChatMessage> getMsg(String userId, String gid, Integer start, Integer page,int ctype) {
		return repository.getMsg(userId, gid,start, page,ctype);
	}*/
	@Override
	public List<ChatMessage> getMsg(String userId, String gid, Integer start, Integer page,int ctype) {
		List<ChatMessage> msgList = repository.getMsg(gid,start, page,ctype);
		List<ChatMessage> map = repository.getMsgDel(userId);
		msgList.removeAll(map);
		
		return msgList;
	}

	@Override
	public ChatMessage findByFAndId(Long f, String id) {
		return repository.findByFromIdAndId(f, id);
	}

	@Override
	public List<ChatMessage> getMsgCallCenter(Long userId, String gid, Integer page, Integer rows,int ctype) {
		List<ChatMessage> msgList = repository.getMsgCallCenter(userId,gid,page,rows,ctype);
		List<ChatMessage> msgList1 = repository.getMsgDel(String.valueOf(userId));
		msgList.removeAll(msgList1);
		return msgList;
	}

	@Override
	public List<ChatMessage> findAllByCt(ArrayList<Long> arrayList) {
		return repository.findAllByCt(arrayList);
	}

	@Override
	public ChatMessage findByfromIdAndtoId(Long userid,String gid,Integer ctype) {
		return repository.findByfromIdAndtoId(userid,gid,ctype);
	}

	@Override
	public ChatMessage findByClientid(String clientid) {
		return repository.findByClientid(clientid);
	}

	@Override
	public List<ChatMessage> findByCtype(int ctype) {
		return repository.findByCtype(ctype);
	}
}
